package test;

import java.util.*;

/**
 * @author 郝文雪
 * @date 2022/9/20 19:43
 * @Description Test
 */
public class 小米2 {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        List<Integer> list=new ArrayList<>();
        while(sc.hasNext()){
            list.add(sc.nextInt());
        }
        int[][] arr=new int[list.size()/2][2];
        for(int i=0;i<arr.length;i=i+2){
            arr[i][0]=list.get(i);
            arr[i][1]=list.get(i+1);
        }
        Arrays.sort(arr,new Comparator<int[]>(){
            @Override
            public int compare(int[] o1,int[] o2){
                if(o1[0]==o2[0]) return o1[1]-o2[1];
                return o1[0]-o2[0];
            }
        });
        int[][] res=new int[arr.length/2][2];
        int min=arr[0][0];
        int max=arr[0][1];
        int m=0;
        for(int i=1;i<arr.length;i++){
            if(max>=arr[i][0]){
                max=Math.max(max,arr[i][1]);

            }else{
                res[m][0]=min;
                res[m][1]=max;
                min=arr[i][0];
                max=arr[i][1];
                m++;
            }
        }
        res[m][0]=min;
        res[m][1]=max;
        int count=0;
        for (int i = 0; i < res.length; i++) {
            count+=res[i][1]-res[i][0];
        }
        System.out.println(count);
    }
}
